from selenium.webdriver.common.by import By

from base.page_base import BasePage
from config import BACK_URL


class BackLogin(BasePage):
    """后台登录页面封装"""
    # 后台登录url
    __back_login_url = BACK_URL
    # 元素定位
    __username = (By.ID, "username")
    __password = (By.ID, "password")
    __img_code = (By.ID, "valicode")
    __login_btn = (By.CLASS_NAME, "login-button")
    # 获取成功登录文本元素
    __success_text = (By.XPATH, "//*[text()='欢迎光临,']")
    # 获取失败登录文本元素
    __fail_text = (By.ID, "errorMessage")

    # 操作步骤
    def open_back_url(self):
        self.driver.get(self.__back_login_url)

    def input_username(self, username):
        """输入用户名"""
        self.base_input(self.__username, username)

    def input_password(self, password):
        """输入密码"""
        self.base_input(self.__password, password)

    def input_img_code(self, img_code="8888"):
        """输入图片验证码"""
        self.base_input(self.__img_code, img_code)

    def click_login_btn(self):
        """点击登录按钮"""
        self.base_click(self.__login_btn)

    def get_result_success_text(self):
        """获取登录成功文本"""
        return self.fd_element(self.__success_text).text

    def get_result_fail_text(self):
        """获取登录失败文本"""
        return self.fd_element(self.__fail_text).text

    # 后台登录业务
    def back_login(self, username, password, img_code="8888"):
        """
        后台登录业务流程
        :param username: 输入用户名
        :param password: 输入密码
        :param img_code: 输入验证码
        :return: 无
        """
        self.input_username(username)
        self.input_password(password)
        self.input_img_code(img_code)
        self.click_login_btn()
